package cn.harmonyoslearn.navrouter.dialog;

import cn.harmonyoslearn.navrouter.datas.enums.InitNavrouterModelEnum;
import cn.harmonyoslearn.navrouter.datas.InitNavInfo;
import cn.harmonyoslearn.navrouter.utils.NotificationUtil;
import com.intellij.openapi.fileChooser.FileChooser;
import com.intellij.openapi.fileChooser.FileChooserDescriptor;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.DialogWrapper;
import com.intellij.openapi.vfs.VirtualFile;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.Objects;

public class InitNavInfoDialog extends DialogWrapper {
    private String navPageNamesPath = "";


    /**
     * 窗口容器
     */
    private JPanel myPanel;
    JPanel namesPathPanel;
    private InitNavrouterModelEnum model = InitNavrouterModelEnum.CUSTOM;
    private final Project project;

    public InitNavInfoDialog(Project project) {
        super(true);
        this.project = project;
        init();
        setTitle("初始化NavRouter");
    }

    public InitNavInfo getInfo() {
        return new InitNavInfo(this.navPageNamesPath, this.model);
    }

    @Override
    protected @Nullable JComponent createCenterPanel() {
        if (myPanel == null) {
            setPanel();
        }
        return myPanel;
    }

    @Override
    protected void doOKAction() {
        if (this.model == InitNavrouterModelEnum.CUSTOM && Objects.equals(this.navPageNamesPath, "")) {
            NotificationUtil.showMessageDialog("提示", "请选择NavPageNames.ts的存放路径");
            return;
        }
        super.doOKAction();
    }

    @Override
    protected void init() {
        super.init();
        // 设置对话框的初始大小
        super.setSize(450, 160); // 宽度为400，高度为300
    }

    private void getFilePath() {
        // 通过按钮选择文件后展示文件路径
        FileChooserDescriptor descriptor = new FileChooserDescriptor(false, true, false, false, false, false);
        descriptor.setTitle("选择NavPageNames的存放路径");
        VirtualFile virtualFile = FileChooser.chooseFile(descriptor, project, null);
        if (virtualFile != null) {
            String path = virtualFile.getPath();
            this.navPageNamesPath = path.replace(Objects.requireNonNull(this.project.getBasePath()), "");
            this.revNamesPathPanel();
        }
    }

    /**
     * 设置容器组件内容
     */
    public void setPanel() {
        myPanel = new JPanel();
        myPanel.setLayout(new BoxLayout(myPanel, BoxLayout.Y_AXIS));
        myPanel.add(Box.createVerticalStrut(15));

        // 选择模式  默认:自定义
        myPanel.add(getModel(), BorderLayout.LINE_START);
        myPanel.add(Box.createVerticalStrut(10));

        // 自定义模式可选择NavPageName.ts文件路径
        namesPathPanel = new JPanel();
        namesPathPanel.setLayout(new BoxLayout(namesPathPanel, BoxLayout.X_AXIS));
        namesPathPanel.setAlignmentX(Component.LEFT_ALIGNMENT);
        namesPathPanel.add(getNamesPathPanel());
        myPanel.add(namesPathPanel, BorderLayout.LINE_START);
        myPanel.add(Box.createVerticalStrut(25));

        myPanel.add(getInfoPanel(), BorderLayout.LINE_START);
        myPanel.add(Box.createVerticalStrut(15));
    }



    private final String defaults = "默认（创建NavRouter模块）";
    private final String custom = "自定义NavPageNames文件位置";
    public JPanel getModel() {
        JPanel panel = new JPanel();
        panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
        panel.setAlignmentX(Component.LEFT_ALIGNMENT);

        // 选择模式单选框
        JRadioButton radioButtonDialog = new JRadioButton(custom);
        radioButtonDialog.setSelected(true);
        JRadioButton radioButtonPage = new JRadioButton(defaults);

        // 将单选按钮添加到面板
        panel.add(radioButtonDialog);
        panel.add(Box.createVerticalStrut(5));
        panel.add(radioButtonPage);

        // 创建按钮组，并将单选按钮添加到该组
        ButtonGroup buttonGroup = new ButtonGroup();
        buttonGroup.add(radioButtonPage);
        buttonGroup.add(radioButtonDialog);

        // 添加 ItemListener 到 JRadioButtons
        ItemListener itemListener = e -> {
            JRadioButton radioButton = (JRadioButton) e.getSource();
            if (e.getStateChange() == ItemEvent.SELECTED) {
                String text = radioButton.getText();

                switch (text) {
                    case defaults -> model = InitNavrouterModelEnum.DEFAULT;
                    case custom -> model = InitNavrouterModelEnum.CUSTOM;
                }
                this.revNamesPathPanel();
            }
        };

        radioButtonPage.addItemListener(itemListener);
        radioButtonDialog.addItemListener(itemListener);
        return panel;
    }

    public void revNamesPathPanel() {
        this.namesPathPanel.removeAll();
        this.namesPathPanel.add(getNamesPathPanel(), Component.LEFT_ALIGNMENT);
        this.namesPathPanel.revalidate();
        this.namesPathPanel.repaint();
        this.myPanel.revalidate();
        this.myPanel.repaint();
    }

    public JPanel getNamesPathPanel() {
        JPanel panel = new JPanel();
        panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));

        if (this.model == InitNavrouterModelEnum.DEFAULT) {
            panel.add(new JLabel("默认路径") {
                @Override
                public Dimension getPreferredSize() {
                    Dimension size = super.getPreferredSize();
                    size.height = 30; // 设置你想要的高度
                    return size;
                }
            });
        } else {
            JLabel label = new JLabel("选择NavPageNames的存放路径: ") {
                @Override
                public Dimension getPreferredSize() {
                    Dimension size = super.getPreferredSize();
                    size.height = 30; // 设置你想要的高度
                    return size;
                }
            };
            panel.add(label);
            panel.add(Box.createHorizontalStrut(8));

            JButton button = new JButton("选择路径");
            panel.add(button);
            button.addActionListener(e -> {
                this.getFilePath();
            });

            panel.add(Box.createHorizontalStrut(25));



            if (!Objects.equals(this.navPageNamesPath, "")) {
                JLabel label1 = new JLabel("路径：" + this.navPageNamesPath);
                panel.add(label1);
            }
        }




        return panel;
    }

    public JPanel getInfoPanel() {
        JPanel panel = new JPanel();
        panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
        panel.setAlignmentX(Component.LEFT_ALIGNMENT);

        JLabel label1 = new JLabel("· 默认模式会创建NavRouter模块，并自动引入@tangs/navrouter库，无需手动配置；");
        JLabel label2 = new JLabel("· 自定义模式可以自定义NavPageName.ts文件路径，不需要创建NavRouter模块；");
        JLabel label3 = new JLabel("· 如果你的项目是一个多模块的项目，推荐使用默认模式，反之请使用自定义模式来创建页面名称文件（NavPageNames.ts）。");
        panel.add(label1);
        panel.add(label2);
        panel.add(label3);
        return panel;
    }
}
